package com.lagou.quartz;

import com.lagou.configBean.ZkClient;
import com.lagou.rpc.RegisterService;
import org.apache.curator.framework.CuratorFramework;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.data.Stat;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

/**
 * @author ascetic
 * @version 1.0
 * @ClassName Ackjob
 * @Description TODO
 * @date 2020-07-03 23:54
 */
public class Ackjob implements Job {


    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        // 每5s 执行一次检查，最后响应时间是不是在5是 以内，如果超过5s ,从zk 中移除节点
        CuratorFramework client = ZkClient.getZKClient();

        Stat stat = null;
        try {
            stat = client.checkExists().forPath(RegisterService.serviceAckTime);

            if(null != stat){
                byte[] bytes = client.getData().forPath(RegisterService.serviceAckTime);
                Long time = Long.valueOf(new String(bytes));
                if(System.currentTimeMillis() - time < 5000){

                    client.setData().forPath(RegisterService.serviceAckTime,
                            Long.valueOf(System.currentTimeMillis()).toString().getBytes());
                }else {
                    client.delete().forPath(RegisterService.serviceAckTime);
                }

            }

        } catch (Exception e) {
            e.printStackTrace();
        }


    }

}